package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;

import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.example.demo.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class UserService {
	
	@Autowired
	private LoadBalancerClient loadBalancerClient;  //ribbon 的负载均衡器
	
	@Autowired
	private RestTemplate restTemplate;
	
	StringBuffer sb = null;
	
	//调用Server-A的服务
	@HystrixCommand(fallbackMethod = "helloFallBack")
	public List<User> getUserList(){
		//选择调用的服务的名称
		//ServiceInstance 封装了服务的基本信息，如 IP，端口
		ServiceInstance si = this.loadBalancerClient.choose("server-b");
		//拼接访问的URL
		sb = new StringBuffer();
		sb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/user");
		
		ParameterizedTypeReference<List<User>> type = new ParameterizedTypeReference<List<User>>() {};
		
		ResponseEntity<List<User>> response =restTemplate.exchange(sb.toString(),HttpMethod.GET,null,type);
		List<User> list = response.getBody();
		return list;
		
	}
	
	//调用Server-DB的服务
	String str = "http://localhost:9002";
	
	//按用户的姓名来查找用户
	@HystrixCommand(fallbackMethod = "helloGetUser")
	public User getUser(String username) {
		ResponseEntity<User> responseEntity = restTemplate.getForEntity(str+"/getUser/{1}",User.class,username);
		User u = responseEntity.getBody();
		return u;
	}
	//按用户的id来查询
	
	@HystrixCommand(fallbackMethod = "helloGetUser1")
	public User getUserById(int id) {
		
		ResponseEntity<User> responseEntity = restTemplate.getForEntity(str+"/getUserById/{1}",User.class,id);
		HttpStatus status = responseEntity.getStatusCode();
		HttpHeaders headers =  responseEntity.getHeaders();
		System.out.println(status+"  "+headers);
		User u = responseEntity.getBody();
		
		return u;
	}
	
	//调用server-DB 的服务，不查数据库
	@HystrixCommand(fallbackMethod = "helloString")
	public String getString() {
		ResponseEntity<String> responseEntity = restTemplate.getForEntity(str+"/getString",String.class);
		
		return responseEntity.getBody();
	}

	public int insertUser(User user) {
		String url = "http://10.10.12.41:9002/insertUser";
		//int num = restTemplate.getForObject("http://localhost:9002/insertUser/{1}/{2}", int.class,user.getUsername(),user.getPassword());
		Integer postForObject = restTemplate.postForObject(url,user,Integer.class);
		
		return postForObject;
	}
	
	public int updateUser(User user) {
		String url = "http://10.10.12.41:9002/updateUser";
	    ResponseEntity<Integer> responseEntity = restTemplate.postForEntity(url, user,Integer.class);
		return responseEntity.getBody();
		
	}
	public List<User> helloFallBack(){
		List<User> user = new ArrayList<User>();
		user.add(new User("用户名不存在","密码不存在",0));
	    return user;
	}
	
	public User helloGetUser(String username){
		User user = new User(username+"不存在","密码不存在",0);
	    return user;
	}
	

	public User helloGetUser1(int id){
		User user = new User("id为"+id+"的用户不存在","密码不存在",0);
	    return user;
	}
	
	public String helloString(){
		
	    return "您访问的Server——DB出问题了，暂时访问不了……"; 
	}
}
